#include "PauseLayer.h"
#include "PlayLayer.h"
#include "AudioManage.h"

static const char* CONST_PNG_PAUSE_MENU = "Pause/pause_menu.png";
static const float CONST_TIME_MOVE = 0.3f;
static const char* CONST_PNG_RESUME_N = "Pause/btn_resume_N.png";
static const char* CONST_PNG_RESUME_S = "Pause/btn_resume_S.png";
static const char* CONST_PNG_RESTART_N = "Pause/btn_restart_N.png";
static const char* CONST_PNG_RESTART_S = "Pause/btn_restart_S.png";
static const char* CONST_PNG_EXIT_N = "Pause/btn_exit_N.png";
static const char* CONST_PNG_EXIT_S = "Pause/btn_exit_S.png";

PauseLayer::PauseLayer()
{
	LayerColor* backcolor = LayerColor::create(Color4B(0, 0, 0, 200));
	this->addChild(backcolor);

	Size visibleSize = Director::getInstance()->getWinSize();
	
	pauseMenuSprite = Sprite::create(CONST_PNG_PAUSE_MENU);
	pauseMenuSprite->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	pauseMenuSprite->setVisible(false);
	this->addChild(pauseMenuSprite);

	float width = pauseMenuSprite->getContentSize().width;
	float height = pauseMenuSprite->getContentSize().height;

	MenuItemImage* resumeBtn = MenuItemImage::create(CONST_PNG_RESUME_N, CONST_PNG_RESUME_S,
		CC_CALLBACK_0(PauseLayer::resumeCallBack, this));
	resumeBtn->setPosition(Vec2(width / 2 , height * 0.7f));

	MenuItemImage* restartBtn = MenuItemImage::create(CONST_PNG_RESTART_N, CONST_PNG_RESTART_S,
		CC_CALLBACK_0(PauseLayer::restartCallBack, this));
	restartBtn->setPosition(Vec2(width / 2, height * 0.5f));

	MenuItemImage* exitBtn = MenuItemImage::create(CONST_PNG_EXIT_N, CONST_PNG_EXIT_S,
		CC_CALLBACK_0(PauseLayer::exitCallBack, this));
	exitBtn->setPosition(Vec2(width / 2, height * 0.3f));

	Menu* pauseMenu = Menu::create(resumeBtn, restartBtn, exitBtn, NULL);
	pauseMenu->setPosition(Vec2::ZERO);

	pauseMenuSprite->addChild(pauseMenu);
}


PauseLayer::~PauseLayer()
{
}

PauseLayer* PauseLayer::thisLayer = nullptr;

void PauseLayer::showLayer()
{
	if (nullptr == thisLayer)
	{
		thisLayer = new PauseLayer();
		PlayLayer::getInstance()->addChild(thisLayer, 20);
		thisLayer->setVisible(false);
	}
	
	PlayLayer::getInstance()->stopGame();
	thisLayer->setVisible(true);
	thisLayer->showMenu();
}

void PauseLayer::disposeLayer()
{
	if (thisLayer != nullptr)
	{
		//delete thisLayer;
		thisLayer = nullptr;
	}	
}

void PauseLayer::showMenu()
{
	auto listener1 = EventListenerTouchOneByOne::create();
	listener1->setSwallowTouches(true);
	listener1->onTouchBegan = CC_CALLBACK_0(PauseLayer::callBack, this);
	m_listener = listener1;

	_eventDispatcher->addEventListenerWithSceneGraphPriority(m_listener, this);

    pauseMenuSprite->setPositionY(pauseMenuSprite->getPositionY() + 80);
    pauseMenuSprite->setOpacity(0);
	pauseMenuSprite->runAction(
        Sequence::create(
            Show::create(),
            Spawn::create(
                          MoveBy::create(CONST_TIME_MOVE, Point(0, -100.0f)),
                          FadeIn::create(CONST_TIME_MOVE),
                          NULL
            ),
            MoveBy::create(CONST_TIME_MOVE, Point(0, 20.0f)),
            NULL
		));
}

bool PauseLayer::callBack()
{
	return true;
}

void PauseLayer::resumeCallBack()
{
	AudioManage::buttonSound();

	_eventDispatcher->removeEventListener(m_listener);
	thisLayer->setVisible(false);

	PlayLayer::getInstance()->continueGame();
}

void PauseLayer::restartCallBack()
{
	AudioManage::buttonSound();

	_eventDispatcher->removeEventListener(m_listener);

	PlayLayer::disposeLayer();

	auto scene = PlayLayer::createScene();
	Director::getInstance()->replaceScene(scene);
}

void PauseLayer::exitCallBack()
{
	Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
